<?php


declare (strict_types=1);

namespace app\common\model\admin;

use cores\BaseModel;
use think\model\relation\BelongsToMany;

/**
 * 超管后台用户模型
 * Class User
 * @package app\common\model\admin
 */
class User extends BaseModel
{
    // 定义表名
    protected $name = 'admin_user';

    // 定义主键
    protected $pk = 'admin_user_id';

    /**
     * 验证用户名是否重复
     * @param string $userName
     * @return bool
     */
    public static function checkExist(string $userName)
    {
        return (bool)static::withoutGlobalScope()
            ->where('user_name', '=', $userName)
            ->where('is_delete', '=', 0)
            ->value('admin_user_id');
    }

    /**
     * 验证手机号码是否重复
     * @param string $phone
     * @return bool
     */
    public static function checkPhoneExist(string $phone)
    {
        return (bool)static::withoutGlobalScope()
            ->where('phone', '=', $phone)
            ->where('is_delete', '=', 0)
            ->value('admin_user_id');
    }

    /**
     * 超管用户信息
     * @param $id
     * @return array|null|static
     */
    public static function detail(int $id)
    {
        return static::get($id);
    }

    /**
     * 关联用户角色表表
     * @return BelongsToMany
     */
    public function role()
    {
        return $this->belongsToMany('Role', 'AdminUserRole');
    }

    /**
     * 关联用户部门表
     * @return BelongsToMany
     */
    public function department()
    {
        return $this->belongsToMany('app\\admin\\model\\system\\Department', 'AdminUserDepartment');
    }

}
